{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Demo of fmm Python API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import Network,NetworkGraph,FastMapMatch,FastMapMatchConfig,UBODT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load network data and graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nodes 17 edges 30\n"
     ]
    }
   ],
   "source": [
    "network = Network(\"../data/edges.shp\")\n",
    "print \"Nodes {} edges {}\".format(network.get_node_count(),network.get_edge_count())\n",
    "graph = NetworkGraph(network)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Precompute an UBODT file\n",
    "\n",
    "**This step can be skipped if you already created one UBODT file.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import UBODTGenAlgorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "ubodt_gen = UBODTGenAlgorithm(network,graph)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time takes 0.009seconds\n"
     ]
    }
   ],
   "source": [
    "status = ubodt_gen.generate_ubodt(\"../data/ubodt.txt\", 4, binary=False, use_omp=True)\n",
    "print status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load UBODT data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "ubodt = UBODT.read_ubodt_csv(\"../data/ubodt.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create FMM model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = FastMapMatch(network,graph,ubodt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define FMM configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 4\n",
    "radius = 0.4\n",
    "gps_error = 0.5\n",
    "fmm_config = FastMapMatchConfig(k,radius,gps_error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run map matching"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Match a single trajectory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "wkt = \"LINESTRING(0.200812146892656 2.14088983050848,1.44262005649717 2.14879943502825,3.06408898305084 2.16066384180791,3.06408898305084 2.7103813559322,3.70872175141242 2.97930790960452,4.11606638418078 2.62337570621469)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = model.match_wkt(wkt,fmm_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matched path:  [8L, 11L, 13L, 18L, 20L, 24L]\n",
      "Matched edge for each point:  [8L, 11L, 18L, 18L, 20L, 24L]\n",
      "Matched edge index  [0, 1, 3, 3, 4, 5]\n",
      "Matched geometry:  LINESTRING(0.20081215 2,1 2,2 2,3 2,3 3,4 3,4 2.6233757)\n",
      "Matched point  LINESTRING(0.20081215 2,1.4426201 2,3 2.1606638,3 2.7103814,3.7087218 3,4 2.6233757)\n"
     ]
    }
   ],
   "source": [
    "print \"Matched path: \", list(result.cpath)\n",
    "print \"Matched edge for each point: \", list(result.opath)\n",
    "print \"Matched edge index \",list(result.indices)\n",
    "print \"Matched geometry: \",result.mgeom.export_wkt()\n",
    "print \"Matched point \", result.pgeom.export_wkt()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Print detailed match information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "candidates = []\n",
    "for c in result.candidates:\n",
    "    candidates.append((c.edge_id,c.source,c.target,c.error,c.length,c.offset,c.spdist,c.ep,c.tp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame(candidates,\n",
    "                  columns=[\"eid\",\"source\",\"target\",\"error\",\"length\",\"offset\",\"spdist\",\"ep\",\"tp\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>eid</th>\n",
       "      <th>source</th>\n",
       "      <th>target</th>\n",
       "      <th>error</th>\n",
       "      <th>length</th>\n",
       "      <th>offset</th>\n",
       "      <th>spdist</th>\n",
       "      <th>ep</th>\n",
       "      <th>tp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>0.140890</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.200812</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.961078</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>0.148799</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.442620</td>\n",
       "      <td>1.241808</td>\n",
       "      <td>0.956684</td>\n",
       "      <td>0.999980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "      <td>0.064089</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.160664</td>\n",
       "      <td>1.718044</td>\n",
       "      <td>0.991819</td>\n",
       "      <td>0.943813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "      <td>0.064089</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.710381</td>\n",
       "      <td>0.549718</td>\n",
       "      <td>0.991819</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20</td>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "      <td>0.020692</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.708722</td>\n",
       "      <td>0.998340</td>\n",
       "      <td>0.999144</td>\n",
       "      <td>0.699640</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   eid  source  target     error  length    offset    spdist        ep  \\\n",
       "0    8       7       8  0.140890     1.0  0.200812  0.000000  0.961078   \n",
       "1   11       8       5  0.148799     1.0  0.442620  1.241808  0.956684   \n",
       "2   18       6      11  0.064089     1.0  0.160664  1.718044  0.991819   \n",
       "3   18       6      11  0.064089     1.0  0.710381  0.549718  0.991819   \n",
       "4   20      11      12  0.020692     1.0  0.708722  0.998340  0.999144   \n",
       "\n",
       "         tp  \n",
       "0  0.000000  \n",
       "1  0.999980  \n",
       "2  0.943813  \n",
       "3  1.000000  \n",
       "4  0.699640  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Match trajectories in a GPS file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fmm import GPSConfig,ResultConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_config = GPSConfig()\n",
    "input_config.file = \"../data/trips.csv\"\n",
    "input_config.id = \"id\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gps file : ../data/trips.csv\n",
      "id column : id\n",
      "geom column : geom\n",
      "timestamp column : timestamp\n",
      "x column : x\n",
      "y column : y\n",
      "GPS point : false\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print input_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result file : ../data/mr.txt\n",
      "Output fields: opath cpath mgeom \n"
     ]
    }
   ],
   "source": [
    "result_config = ResultConfig()\n",
    "result_config.file = \"../data/mr.txt\"\n",
    "result_config.output_config.write_opath = True\n",
    "print result_config.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "status = model.match_gps_file(input_config, result_config, fmm_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time takes 0.05 seconds\n",
      "Total points 17 matched 17\n",
      "Map match speed 340 points/s \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
